﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using App.Data;
using App.Core.Extensions;
using App.Core.DB;

public partial class Pages_Private_EventList : BasePage
{
    private bool _IsDeleteMode = false;
    private int _ID;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            CheckAndDelete();
            BindEventsList(1);
        }
    }
    protected void BindPageInfo()
    {
        _ID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        if (String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.DELETE), "True", false) == 0)
            _IsDeleteMode = true;

        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void CheckAndDelete()
    {
        if (_IsDeleteMode)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            var Event = context.Events.FirstOrDefault(P => P.ID == _ID);
            if (Event == null)
                WebUtil.ShowMessageBox(divMessage, "Sorry! Requested Event was not found for delete.", true);
            else
            {
                try
                {
                    //context.Events.DeleteObject(Event);                
                    context.DeleteObject(Event);
                    context.SaveChanges();
                    WebUtil.ShowMessageBox(divMessage, "Event deleted Successfully.", false);
                }
                catch
                {
                    WebUtil.ShowMessageBox(divMessage, "Sorry! This Event has related information. Delete failed.", true);
                }
            }
        }
    }
    protected void BindEventsList(int pageNumber)
    {        
        UtilityDAO dao = new UtilityDAO();
        int totalRecord = 0;
        DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_ALL_EVENTS, null, pageNumber, ConfigReader.EventBookingListSize, out totalRecord);
        //ucEventList.DataSource = ds.Tables[0];
        //ucEventList.EditLink = AppConstants.Pages.EVENT_CHANGE + "?" + AppConstants.QueryString.ID + "={0}";
        //ucEventList.DeleteLink = AppConstants.Pages.EVENT_LIST + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.DELETE + "=True";
        //ucEventList.DeleteMessage = "Are you sure you want to delete this contact?";
        //ucEventList.NoRecordMessgae = "No Events found.";
        //ucEventList.DataBind();        
        rptEventList.DataSource = ds.Tables[0];
        rptEventList.DataBind();

        ///Bind the Pager Control
        ucEventListPager.TotalRecord = totalRecord;
        ucEventListPager.PageNo = pageNumber;
        ucEventListPager.PageSize = ConfigReader.EventBookingListSize;
        ucEventListPager.TotalRecordMessage = "Total {0} Events Found.";
        ucEventListPager.DataBind();
    }
    protected String GetFormattedData(object data)
    {
        if (data.GetType() == typeof(String))
        {
            String text = NullHandler.GetString(data);
            if (text.WordCount() > 30)
                text = text.GetWords(30);
            return WebUtil.FormatText(text);
        }
        else
            return WebUtil.FormatData(data);
    }
    protected void ucEventListPager_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindEventsList(e.PageIndex);
    }
}